package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;

import server.Message;


/**
 * 
 * Client-side handler that manages commands sent through
 * the client controller and executes them in the client GUI
 *
 */
public class ClientHandler implements Runnable {

	private static Socket socket;
	private int port;
	private String hostName;
	private static BufferedReader reader;
	private static PrintWriter writer;
	private static BlockingQueue<String> commandQueue;
	private final MetaDeckController controller;


	public ClientHandler(BufferedReader reader, PrintWriter writer, MetaDeckController controller){
		this("localhost", 4444, reader, writer, controller);
	}

	public ClientHandler(String hostName, int port, BufferedReader reader, PrintWriter writer, MetaDeckController controller){
		this.hostName = hostName;
		this.port = port;
		this.reader = reader;
		this.writer = writer;
		this.controller = controller;
	}

	public void run() {
		Thread readT = new Thread(new Runnable(){
			public void run(){
				try{
					String line = reader.readLine();				
					while (line != null){
						accept(line);
						line = reader.readLine();
					}
					reader.close();
				} catch (IOException e){
					e.getStackTrace();
				}
			}
		});
		readT.start();
	}
	
	public void close() throws IOException {
		System.out.println("in client handler");
		writer.close();
		reader.close();
		socket.close();
		
	}

	public static void writeLine(String output){
		System.out.println("writing " + output);
		writer.println(output);
	}
	
	public String readLine() throws IOException{
		return reader.readLine(); 
	}
	
	// login
	public static void loginRequest(String username){
		writeLine(Message.loginRequest(username));
	} 

	// message
	public static void chatMessage(String sender, String text){
		writeLine(Message.chatMessage(sender, text));
	}
	
	public static void startDrag(int stackName, String draggerName)
	{
		writeLine(Message.startDrag(Integer.toString(stackName), draggerName));
	}
	
	
	
	/**
	 * Parses and handles message input
	 * @param message
	 */
	public void accept(String message){
		
/*		//single chat
		if (message.startsWith("chat request successful")){			
		String username = message.substring(message.indexOf("!") + 1);
		System.out.println("chat request successful"+username);
		gui.chatRequestSuccess(username);
		}
		else if (message.startsWith("chat request failure")){
		String username = message.substring(message.indexOf("!") + 1);
		gui.chatRequestFail(username);
		}
		//single chat request from sender
		if (message.startsWith("chatRequest")){			
			String[] temp = message.substring(12, message.length() -1).split(",");
			String sender = temp[0];
			String receiver = temp[1];
			gui.chatRequestFrom(sender);                       
		}*/		
//		//single chat accepted
//		if (message.startsWith("chat accepted successful")){
//		String username = message.substring(message.indexOf("!") + 1);
//		gui.chatAcceptedSuccess(username);
//		}	
//		else if (message.startsWith("chat accepted failure")){
//		String username = message.substring(message.indexOf("!") + 1);
//		gui.chatAcceptedFailure(username);
//		}
//		//single chat declined
//		if (message.startsWith("chat declined successful")){
//		String username = message.substring(message.indexOf("!") + 1);
//		gui.chatDeclinedSuccess(username);
//		}
//		else if (message.startsWith("chat declined failure")){
//		String username = message.substring(message.indexOf("!") + 1);
//		gui.chatDeclinedFailure(username);
//		}

		//single chat message
		if (message.startsWith("chatMessage")){
			System.out.println("msg in handler " + message);
			String[] sub = message.substring(12, message.length() -1).split("%");

			String sender = sub[0];
			String text = sub[1];
			
			controller.getChat().acceptMessage(sender, text);
		}
		//single chat success
		if (message.startsWith("chat message successful")){
			String[] temp = message.substring(message.indexOf("!")+1).split("%",2);
			System.out.println(temp.length+"MOFUUU "+message.substring(message.indexOf("!")+1));
			String username = temp[0];
			String text = temp[1];			
			//gui.chatMessageSuccessful(username, text);
		}
		
		if (message.startsWith("startDrag"))
		{
			String[] params = message.split("(")[1].split(",");
			int stackID = Integer.parseInt(params[0]);
			String sender = params[1].substring(0, params[1].length()-1);
				
			//gui.
		}


	//have methods that just put string on blocking queue
	// returns success/failure as string?

	//have methods that server can call on which calls methods on gui


	}


}
